//
//  LoginPageViewController.m
//  SchoolTimetable
//
//  Created by 梦里都是梦 on 16/1/6.
//  Copyright (c) 2016年 Yuanlinqian. All rights reserved.
//

#import "LoginPageViewController.h"
#import "RegisterPageViewController.h"
#import "RootTabBarController.h"
#import "PersonPageViewController.h"

@interface LoginPageViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *barImgView;
@property (weak, nonatomic) IBOutlet UITextField *userNameTF;
@property (weak, nonatomic) IBOutlet UITextField *passwordTF;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
@property (weak, nonatomic) IBOutlet UIView *whiteBackView;

@end

@implementation LoginPageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UINavigationBar * naviBar = self.navigationController.navigationBar;
    naviBar.barTintColor = [UIColor colorWithRed:233/255.f green:215/255.f blue:186/255.f alpha:1];
    self.whiteBackView.layer.borderColor =[UIColor colorWithRed:222/255.f green:214/255.f blue:217/255.f alpha:1].CGColor;
    // Do any additional setup after loading the view.
}
- (IBAction)loginBtnPressed:(UIButton *)sender {
    [SVProgressHUD showWithStatus:@"loaging..." maskType:SVProgressHUDMaskTypeClear];
    
    [BmobUser loginWithUsernameInBackground:self.userNameTF.text
                                   password:[Tools MD5StringFromString:self.passwordTF.text] block:^(BmobUser *user, NSError *error) {
                                       if (user) {
                                           [SVProgressHUD showSuccessWithStatus:@"登陆成功"];
                                           [[NSNotificationCenter defaultCenter]postNotificationName:USER_REFRESH_USERINFO object:nil];
                                           dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                                               [AnimationTool setAnimationWith:kCATransitionPush and:kCATransitionFromLeft andTime:0.7 with:self];
                                               [self.navigationController presentViewController:[[RootTabBarController alloc]init] animated:NO completion:nil];
                                           });
                                       }
                                       else
                                       {
                                           [SVProgressHUD showErrorWithStatus:@"登陆失败"];
                                       }
                                   }];
    
    

}
- (IBAction)registerTapPressed:(UITapGestureRecognizer *)sender {
    RegisterPageViewController * registerVc = [self.storyboard instantiateViewControllerWithIdentifier:@"RegisterPageViewController"];
    [AnimationTool setAnimationWith:kCATransitionPush and:kCATransitionFromRight andTime:0.7 with:self];
    [self.navigationController pushViewController:registerVc animated:NO];

}
-(void)pressedToRootViewController
{
    [AnimationTool setAnimationWith:kCATransitionPush and:kCATransitionFromLeft andTime:0.7 with:self];
    [self.navigationController popToRootViewControllerAnimated:YES];
   
}
- (IBAction)backBarButtonItemPressed:(UIButton *)sender {
    [self.navigationController popToRootViewControllerAnimated:YES];
    if (_htblock) {
        _htblock();
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
